package com.example.lx5_22

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.lx5_22.databinding.ActivityLoginBinding
import com.example.lx5_22.user.UserViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/LoginActivity")
class LoginActivity : BaseActivity<ActivityLoginBinding,UserViewModel>() {
    override fun initView() {
        val longTime = SPUtils.getInstance().getLong("time", 0)
        val currentTime = System.currentTimeMillis()
        val day = (currentTime - longTime) / 1000 / 60 / 60 / 24
        if(day < 7){
            start()
        }

        binding.btnLog.setOnClickListener {
            val uname = binding.etUsername.text.toString()
            val password = binding.etPassword.text.toString()
            if (uname.isNullOrEmpty() || password.isNullOrEmpty()) {
                ToastUtils.showLong("输入内容不能为空")
                return@setOnClickListener
            }
            viewModel.login(uname,password)
        }
    }

    fun start(){
        ARouter.getInstance().build("/app/SplashActivity").navigation()
        finish()
    }

    override fun initData() {
        viewModel.loginData.observe(this){
            ToastUtils.showLong("登录成功")
            SPUtils.getInstance().apply {
                put("token",it.token)
                put("username",it.username)
                put("id",it.id)
                put("roleid",it.roleid)
                put("time",System.currentTimeMillis())
            }
            start()
        }
    }

}